<?php

namespace App\Admin\Controllers;

use App\Models\ClassCourse;
use App\Models\Grade;
use App\Models\Teacher;
use App\Models\Course;
use App\Models\Semester;
use App\admin\Extensions\InsertScore;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;


use Encore\Admin\Facades\Admin;


class TeacherCourseController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('教学管理')
            ->description('我的课程')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('教学管理')
            ->description('查看课程')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('教学管理')
            ->description(' ')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('教学管理')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ClassCourse);

        $user_id = Admin::user()->id;
        $teacher = Teacher::where('user_id',$user_id)->first();
        $teacher_id = $teacher->id;
        $grid->model()->where('course_teacher', $teacher_id);

//        $grid->model()->orderBy('id','desc');
        $grid->id('Id')->sortable();

        $grid->column('semester.name','学期');

        $grid->column('class.class_name','班级');
        $grid->column('class.class_teacher','班主任')->display(function ($teacher_id) {
            $teacher = Teacher::where('id', $teacher_id)->first();
            return $teacher->name;
        });
        $grid->column('course.course_name','课程');
        $grid->column('course.credit', '学分');
        $grid->column('course.total', '总分');

        $states = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'primary'],
            'off' => ['value' => 2, 'text' => '否', 'color' => 'default'],
        ];
        $grid->end('是否结课')->switch($states);

//        $grid->created_at('创建日期');
//        $grid->updated_at('修改日期');
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();

            // 添加操作
            $actions->append(new InsertScore($actions->getKey(),$actions->row));
        });
        $grid->paginate(10);
        $grid->disableCreateButton();
//        $grid->disableActions();
//        $grid->disableFilter();
        $grid->disableRowSelector();

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器

            $grades = Grade::get();
            $grade_arr = [];
            foreach ($grades as $grade) {
                $grade_arr[$grade->id] = $grade->class_name;
            }

            $filter->equal('class_id', '班级名称')->select($grade_arr);

            $courses = Course::get();
            $course_arr = [];
            foreach ($courses as $course) {
                $course_arr[$course->id] = $course->course_name;
            }

            $filter->equal('course_id','课程名称')->select($course_arr);

            $filter->equal('end', '是否结课')->radio([
                0    => ' 否',
                1    => ' 是',
            ]);

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ClassCourse::findOrFail($id));

//        $show->name('学期名称');
//        $show->start_date('开始日期');
//        $show->end_date('结束日期');
//        $show->is_semester('Is semester');
//        $show->created_at('Created at');
//        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ClassCourse);

        $states = [
            'on'  => ['value' => 1, 'text' => '是'],
            'off' => ['value' => 0, 'text' => '否'],
        ];
        $form->switch('end','是否节课')->states($states);

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
//            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        return $form;
    }
}
